import { msg } from '../../components/notice/notice';

Template.notifications_list_item.helpers({
  isType: function(type) {
    if (type == Template.instance().data.notification.type) {
      return true;
    } else {
      return false;
    }
  },
  tooltipTitle: function() {
    if (Template.instance().data.notification.read) {
      return '标记为未读';
    } else {
      return '标记为已读';
    }
  }
});

Template.notifications_list_item.events({
  'click .dot-mark'(e, instance) {
    if (instance.data.notification.read) {
      Meteor.call('markUnread', instance.data.notification._id);
    } else {
      Meteor.call('markRead', instance.data.notification._id);
    }
  },

  'click .confirm-make-friend'(e, instance) {
    Meteor.call('makeFriendSuccess', instance.data.notification.details.user_id, instance.data.notification._id ,(err, res) => {
      if (res == 'is-friend') {
        msg({content: '你们已经是好友了'});
      }
    });
  },

  'click .delete-make-friend'(e, instance) {
    Meteor.call('deleteMakeFriendRequest', instance.data.notification._id);
  },

  'click .confirm-join-channel'(e, instance) {
    let options = {
      _id: instance.data.notification.details._id,
      user_id: instance.data.notification.details.user_id,
      channelId: instance.data.notification.details.channelId,
      notification_id: instance.data.notification._id
    };
    Meteor.call('confirmJoin', options);
  },

  'click .delete-join-channel'(e, instance) {
    let options = {
      _id: instance.data.notification._id
    };
    Meteor.call('deleteJoin', options);
  },

  'click .confirm-invite'(e, instance) {
    let options = {
      id: instance.data.notification._id,
      channelId: instance.data.notification.details._id
    };
    Meteor.call('addMember', options, (err, res) => {
      if (res) {
        msg({content: '加入成功'});
      }
      Router.go(`/channel/${instance.data.notification.details.channelId}`);
    });
  },

  'click .delete-invite'(e, instance) {
    Meteor.call('deleteNotification', instance.data.notification._id);
  },

});
